namespace SmartSoft.SystemForms
{
    using SmartSoft;
    using SmartSoft.Controls;
    using Stimulsoft.Report;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FormCreateActiveCode : FormControl
    {
        private BtnControl btnActiveCode;
        private BtnControl btnCancel;
        private BtnControl btnOk;
        private BtnControl btnPathBackup;
        private IContainer components;
        private LabelControl lblNgay;
        private LabelControl lblPathBackup;
        private LabelControl lblSo_Luong;
        private TextDate txtNgay;
        private TextControl txtPathBackup;
        private TextNumeric txtSo_Luong;

        public FormCreateActiveCode()
        {
            this.InitializeComponent();
            this.txtNgay.Value = DateTime.Today;
            this.txtSo_Luong.NumericValue = 1000M;
        }

        private void btnActiveCode_Click(object sender, EventArgs e)
        {
            if (this.txtNgay.Value.Equals(DBNull.Value) || this.txtNgay.Value.Equals(SDBNull.Value) || (this.txtSo_Luong.NumericValue == 0M))
            {
                MessageBox.Show("Chưa đưa đủ Thông tin ngày tháng v\x00e0 số lượng!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                DataTable dataTable = new DataTable("CardNumber");
                DataRow row = null;
                DataColumn column = new DataColumn();
                column.ColumnName = "CardNumber";
                column.DataType = System.Type.GetType("System.String");
                column.MaxLength = 0x18;
                dataTable.Columns.Add(column);
                dataTable.PrimaryKey = new DataColumn[] { column };
                string key = "";
                for (int i = 0; i < this.txtSo_Luong.NumericValue; i++)
                {
                    key = AppInstance.SoftwareEncode(0x89);
                    while (dataTable.Rows.Find(key) != null)
                    {
                        key = AppInstance.SoftwareEncode(0x89);
                    }
                    row = dataTable.NewRow();
                    row["CardNumber"] = key;
                    dataTable.Rows.Add(row);
                }
                StiReport report = new StiReport();
                report.DataStore.Clear();
                report.RegData("CardNumber", dataTable);
                report.Load(@"..\..\ReportFiles\CardNumber.mrt");
                report.Show();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtNgay.Value.Equals(DBNull.Value) || this.txtNgay.Value.Equals(SDBNull.Value) || (this.txtSo_Luong.NumericValue == 0M))
            {
                MessageBox.Show("Chưa đưa đủ Thông tin ngày tháng v\x00e0 số lượng!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                DataTable table = new DataTable("CardNumber");
                DataRow row = null;
                DataColumn column = new DataColumn();
                column.ColumnName = "CardNumber";
                column.DataType = System.Type.GetType("System.String");
                column.MaxLength = 0x18;
                DataColumn column2 = new DataColumn();
                column2.ColumnName = "CreatedDate";
                column2.DataType = System.Type.GetType("System.DateTime");
                DataColumn column3 = new DataColumn();
                column3.ColumnName = "TaxCode";
                column3.DataType = System.Type.GetType("System.String");
                column3.MaxLength = 0x80;
                DataColumn column4 = new DataColumn();
                column4.ColumnName = "LocalName";
                column4.DataType = System.Type.GetType("System.String");
                column4.MaxLength = 0x80;
                DataColumn column5 = new DataColumn();
                column5.ColumnName = "EnglishName";
                column5.DataType = System.Type.GetType("System.String");
                column5.MaxLength = 0x80;
                DataColumn column6 = new DataColumn();
                column6.ColumnName = "LocalAddress";
                column6.DataType = System.Type.GetType("System.String");
                column6.MaxLength = 0x80;
                DataColumn column7 = new DataColumn();
                column7.ColumnName = "EnglishAddress";
                column7.DataType = System.Type.GetType("System.String");
                column7.MaxLength = 0x80;
                DataColumn column8 = new DataColumn();
                column8.ColumnName = "RegisteredFile";
                column8.DataType = System.Type.GetType("System.String");
                table.Columns.Add(column);
                table.Columns.Add(column2);
                table.Columns.Add(column3);
                table.Columns.Add(column4);
                table.Columns.Add(column5);
                table.Columns.Add(column6);
                table.Columns.Add(column7);
                table.Columns.Add(column8);
                table.PrimaryKey = new DataColumn[] { column };
                DataTable dataTable = new DataTable("NewCardNumber");
                DataRow row2 = null;
                DataColumn column9 = new DataColumn();
                column9.ColumnName = "CardNumber";
                column9.DataType = System.Type.GetType("System.String");
                column9.MaxLength = 0x18;
                dataTable.Columns.Add(column9);
                if (File.Exists(this.txtPathBackup.Text + @"\CardNumber.xml"))
                {
                    table.ReadXml(this.txtPathBackup.Text + @"\CardNumber.xml");
                }
                string key = "";
                for (int i = 0; i < this.txtSo_Luong.NumericValue; i++)
                {
                    key = AppInstance.SoftwareEncode(0x8b);
                    while (table.Rows.Find(key) != null)
                    {
                        key = AppInstance.SoftwareEncode(0x8b);
                    }
                    row = table.NewRow();
                    row["CreatedDate"] = this.txtNgay.Value;
                    row["CardNumber"] = key;
                    table.Rows.Add(row);
                    row2 = dataTable.NewRow();
                    row2["CardNumber"] = key;
                    dataTable.Rows.Add(row2);
                }
                StiReport report = new StiReport();
                report.DataStore.Clear();
                report.RegData("CardNumber", dataTable);
                report.Load(@"..\..\ReportFiles\CardNumber.mrt");
                report.Show();
                if (MessageBox.Show("Bạn có muốn lưu danh s\x00e1ch mã số này không?", AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    table.WriteXml(this.txtPathBackup.Text + @"\CardNumber.xml");
                }
            }
        }

        private void btnPathBackup_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            dialog.ShowDialog();
            this.txtPathBackup.Text = dialog.SelectedPath;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormCreateActiveCode));
            this.txtNgay = new TextDate();
            this.lblNgay = new LabelControl();
            this.txtSo_Luong = new TextNumeric();
            this.lblSo_Luong = new LabelControl();
            this.btnOk = new BtnControl();
            this.btnCancel = new BtnControl();
            this.btnPathBackup = new BtnControl();
            this.lblPathBackup = new LabelControl();
            this.txtPathBackup = new TextControl();
            this.btnActiveCode = new BtnControl();
            base.SuspendLayout();
            this.txtNgay.DateTimeFormat = TextDate.DateTimeFormatType.Date;
            this.txtNgay.Font = new Font("Arial", 8.25f);
            this.txtNgay.InsertKeyMode = InsertKeyMode.Overwrite;
            this.txtNgay.Location = new Point(0x6d, 12);
            this.txtNgay.Mask = "00/00/0000";
            this.txtNgay.Name = "txtNgay";
            this.txtNgay.OldValue = null;
            this.txtNgay.Size = new Size(100, 20);
            this.txtNgay.TabIndex = 0;
//            //this.txtNgay.Value = null;//manager.GetObject("txtNgay.Value");
            this.lblNgay.AutoSize = true;
            this.lblNgay.Font = new Font("Arial", 8.25f);
            this.lblNgay.Location = new Point(12, 15);
            this.lblNgay.Name = "lblNgay";
            this.lblNgay.Size = new Size(0x31, 14);
            this.lblNgay.TabIndex = 1;
            this.lblNgay.Text = "Ngày lập";
            this.txtSo_Luong.Font = new Font("Arial", 8.25f);
            this.txtSo_Luong.Location = new Point(0x6d, 0x26);
            this.txtSo_Luong.Name = "txtSo_Luong";
            this.txtSo_Luong.NumericScaleOnFocus = 0;
            this.txtSo_Luong.NumericScaleOnLostFocus = 0;
            int[] bits = new int[4];
            this.txtSo_Luong.NumericValue = new decimal(bits);
            this.txtSo_Luong.OldValue = null;
            this.txtSo_Luong.Size = new Size(100, 20);
            this.txtSo_Luong.TabIndex = 1;
            this.txtSo_Luong.Text = "0";
            this.txtSo_Luong.TextAlign = HorizontalAlignment.Right;
//            //this.txtSo_Luong.Value = null;//manager.GetObject("txtSo_Luong.Value");
            this.txtSo_Luong.ZeroIsShown = true;
            this.lblSo_Luong.AutoSize = true;
            this.lblSo_Luong.Font = new Font("Arial", 8.25f);
            this.lblSo_Luong.Location = new Point(12, 0x29);
            this.lblSo_Luong.Name = "lblSo_Luong";
            this.lblSo_Luong.Size = new Size(0x33, 14);
            this.lblSo_Luong.TabIndex = 1;
            this.lblSo_Luong.Text = "Số lượng";
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0xf5, 130);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(0x70, 0x17);
            this.btnOk.TabIndex = 5;
            this.btnOk.Text = "&Lập mã thẻ";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(0x16b, 130);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x70, 0x17);
            this.btnCancel.TabIndex = 6;
            this.btnCancel.Text = "&Huỷ bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnPathBackup.Font = new Font("Arial", 8.25f);
            this.btnPathBackup.Location = new Point(0x1be, 0x3f);
            this.btnPathBackup.Name = "btnPathBackup";
            this.btnPathBackup.Size = new Size(0x1d, 0x17);
            this.btnPathBackup.TabIndex = 3;
            this.btnPathBackup.TabStop = false;
            this.btnPathBackup.Text = "...";
            this.btnPathBackup.UseVisualStyleBackColor = true;
            this.btnPathBackup.Click += new EventHandler(this.btnPathBackup_Click);
            this.lblPathBackup.AutoSize = true;
            this.lblPathBackup.Font = new Font("Arial", 8.25f);
            this.lblPathBackup.Location = new Point(12, 0x43);
            this.lblPathBackup.Name = "lblPathBackup";
            this.lblPathBackup.Size = new Size(0x3e, 14);
            this.lblPathBackup.TabIndex = 6;
            this.lblPathBackup.Text = "Đường dẫn";
            this.txtPathBackup.CheckTextSearched = false;
            this.txtPathBackup.DisabledForeColor = Color.Blue;
            this.txtPathBackup.Font = new Font("Arial", 8.25f);
            this.txtPathBackup.Location = new Point(0x6d, 0x40);
            this.txtPathBackup.Name = "txtPathBackup";
            this.txtPathBackup.OldValue = null;
            this.txtPathBackup.Size = new Size(0x14b, 20);
            this.txtPathBackup.TabIndex = 2;
//            //this.txtPathBackup.Value = null;//manager.GetObject("txtPathBackup.Value");
            this.btnActiveCode.Font = new Font("Arial", 8.25f);
            this.btnActiveCode.Location = new Point(0x7f, 130);
            this.btnActiveCode.Name = "btnActiveCode";
            this.btnActiveCode.Size = new Size(0x70, 0x17);
            this.btnActiveCode.TabIndex = 4;
            this.btnActiveCode.Text = "Lập mã &k\x00edch hoạt";
            this.btnActiveCode.UseVisualStyleBackColor = true;
            this.btnActiveCode.Click += new EventHandler(this.btnActiveCode_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x1e7, 0xa5);
            base.Controls.Add(this.btnPathBackup);
            base.Controls.Add(this.lblPathBackup);
            base.Controls.Add(this.txtPathBackup);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnActiveCode);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.txtSo_Luong);
            base.Controls.Add(this.lblSo_Luong);
            base.Controls.Add(this.lblNgay);
            base.Controls.Add(this.txtNgay);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormCreateActiveCode";
            this.Text = "Lập mã thẻ kh\x00e1ch hàng";
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

